package com.huadao.pda.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanManager;
import android.device.scanner.configuration.PropertyID;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;

public abstract class BaseScanDialogFragment extends BaseDialogFragment implements OnScanResult {

//    private final static String SCAN_ACTION = ScanManager.ACTION_DECODE;//default action
//    private Vibrator mVibrator;
//    protected ScanManager mScanManager;
//    private SoundPool soundpool = null;
//    private int soundid;
//    private String barcodeStr;
//    private boolean isScaning = false;

//    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            // TODO Auto-generated method stub
//            isScaning = false;
//            soundpool.play(soundid, 1, 1, 0, 0, 1);
////            showScanResult.setText("");
//            mVibrator.vibrate(100);
//
//            byte[] barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
//            int barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
//            byte temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, (byte) 0);
//            android.util.Log.i("debug", "----codetype--" + temp);
//            barcodeStr = new String(barcode, 0, barcodelen);
//
//            BaseScanDialogFragment.this.onScanResult(barcodeStr);
//
////            showScanResult.append(" length："  +barcodelen);
////            showScanResult.append(" barcode："  +barcodeStr);
//            //showScanResult.setText(barcodeStr);
//
//        }
//
//    };
//
//    private void initScan() {
//        // TODO Auto-generated method stub
//        mScanManager = new ScanManager();
//        mScanManager.openScanner();
//
//        mScanManager.switchOutputMode( 0);
//        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
//        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
//    }
//
//    @Override
//    public void onPause() {
//        // TODO Auto-generated method stub
//        super.onPause();
////        if(getActivity() instanceof BaseScanActivity){
////
////        }else {
////            if (mScanManager != null) {
////                mScanManager.stopDecode();
////                isScaning = false;
////            }
////            getActivity().unregisterReceiver(mScanReceiver);
////        }
//        if (mScanManager != null) {
//            mScanManager.stopDecode();
//            isScaning = false;
//        }
//        getActivity().unregisterReceiver(mScanReceiver);
//    }
//
//    @Override
//    public void onResume() {
//        // TODO Auto-generated method stub
//        super.onResume();
//        if(getActivity() instanceof BaseScanActivity){
//
//        }else {
//            initScan();
////        showScanResult.setText("");
//            IntentFilter filter = new IntentFilter();
//            int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG};
//            String[] value_buf = mScanManager.getParameterString(idbuf);
//            if (value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
//                filter.addAction(value_buf[0]);
//            } else {
//                filter.addAction(SCAN_ACTION);
//            }
//
//            getActivity().registerReceiver(mScanReceiver, filter);
//        }
//    }

    protected void startScan(){
        if(getActivity() instanceof BaseScanActivity){
            ((BaseScanActivity) getActivity()).startScan();
        }else {

        }
//        mScanManager.stopDecode();
//        isScaning = true;
//        try {
//            Thread.sleep(100);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        mScanManager.startDecode();
    }

    protected void stopScan(){
        if(getActivity() instanceof BaseScanActivity){
            ((BaseScanActivity) getActivity()).stopScan();
        }else {

        }
//        mScanManager.stopDecode();
    }
}
